/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.Rep;

import session.TX.GroupBuyingManagerSBRemote;
import managedBean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import entity.GroupBuyingItem;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "groupBuyingManagedBean")
@RequestScoped
public class GroupBuyingMB {

    @ManagedProperty(value = "#{fakeMainPortalMB}")
    private FakeMainPortalMB portalManagedBean;
    @EJB
    GroupBuyingManagerSBRemote groupBuyingBean;
    Double originalPrice;
    Double offeredPrice;
    Date effectiveStart;
    Date effectiveEnd;
    Date redemptionEnd;
    Date redemptionStart;
    Integer minQuota;
    Integer maxQuota;
    Integer personalQuota;
    String requireBookingStr;
    String validateRequirements;
    String description;
    String name;
    String statusMsg = "";

    /** Creates a new instance of GroupBuyingMB */
    public GroupBuyingMB() {
    }

    public static Boolean strToBoolean(String s) {
        if (s.equalsIgnoreCase("yes")) {
            return true;
        } else {
            return false;
        }
    }

    public static Date strToDate(String s) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        try {
            Date date = sdf.parse(s);
            return date;
        } catch (Exception e) {
            return null;
        }
    }

    public void addNewProposal(ActionEvent event) {
        Date date = new Date();

        long diff = (effectiveEnd.getTime() - effectiveStart.getTime()) / (1000 * 60 * 60 * 24);
        long diff2 = (effectiveStart.getTime() - date.getTime()) / (1000 * 60 * 60 * 24);

//        if (effectiveStart.before(date)) {
//            statusMsg = "effective start date not valid!";
//            return;
//        } else 
        if (effectiveEnd.before(effectiveStart)) {
            statusMsg = "effective period not valid!";
        } else if (redemptionEnd.before(redemptionStart)) {
            statusMsg = "redemption period not valid!";
        } else if (diff > 7 || diff < 1) {
            statusMsg = "effective period cannot be over 7 days or less than a day!";
            return;
        } else if (diff2 > 365) {
            statusMsg = "effective start cannot be over one year after today!";
            return;
        }
        statusMsg = groupBuyingBean.addGroupBuyingItem(originalPrice, offeredPrice, effectiveEnd,
                effectiveStart, redemptionEnd, redemptionStart,
                minQuota, maxQuota, personalQuota, strToBoolean(requireBookingStr), description,
                validateRequirements, name, portalManagedBean.getStakeholderName());
    }

    public void deleteProposal(ActionEvent event) {
        Long id = (Long) event.getComponent().getAttributes().get("gbItemId");
        groupBuyingBean.deleteProposal(id);
    }

    public void approveProposal(ActionEvent event) {
        Long id = (Long) event.getComponent().getAttributes().get("gbItemId");
        groupBuyingBean.approveProposal(id);
    }

    public void denyProposal(ActionEvent event) {
        Long id = (Long) event.getComponent().getAttributes().get("gbItemId");
        groupBuyingBean.denyProposal(id);
    }

    public List<GroupBuyingItem> getExistingProposal() {
        return groupBuyingBean.getStakeholderGBItem(portalManagedBean.getStakeholderName());
    }
    
    public List<GroupBuyingItem> getAllProposal(){
        return groupBuyingBean.getGBItem();
    }

    public Integer getMaxQuota() {
        return maxQuota;
    }

    public void setMaxQuota(Integer maxQuota) {
        this.maxQuota = maxQuota;
    }

    public Integer getMinQuota() {
        return minQuota;
    }

    public void setMinQuota(Integer minQuota) {
        this.minQuota = minQuota;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getOfferedPrice() {
        return offeredPrice;
    }

    public void setOfferedPrice(Double offeredPrice) {
        this.offeredPrice = offeredPrice;
    }

    public Double getOriginalPrice() {
        return originalPrice;
    }

    public void setOriginalPrice(Double originalPrice) {
        this.originalPrice = originalPrice;
    }

    public String getRequireBookingStr() {
        return requireBookingStr;
    }

    public void setRequireBookingStr(String requireBooking) {
        this.requireBookingStr = requireBooking;
    }

    public String getStakeholderName() {
        return portalManagedBean.getStakeholderName();
    }
    
    public String getValidateRequirements() {
        return validateRequirements;
    }

    public void setValidateRequirements(String validateRequirements) {
        this.validateRequirements = validateRequirements;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public Integer getPersonalQuota() {
        return personalQuota;
    }

    public void setPersonalQuota(Integer personalQuota) {
        this.personalQuota = personalQuota;
    }

    public Date getEffectiveStart() {
        return effectiveStart;
    }

    public void setEffectiveStart(Date effectiveStart) {
        this.effectiveStart = effectiveStart;
    }

    public Date getEffectiveEnd() {
        return effectiveEnd;
    }

    public void setEffectiveEnd(Date effectiveEnd) {
        this.effectiveEnd = effectiveEnd;
    }

    public Date getRedemptionEnd() {
        return redemptionEnd;
    }

    public void setRedemptionEnd(Date redemptionEnd) {
        this.redemptionEnd = redemptionEnd;
    }

    public Date getRedemptionStart() {
        return redemptionStart;
    }

    public void setRedemptionStart(Date redemptionStart) {
        this.redemptionStart = redemptionStart;
    }
    
    /**
     * @param portalManagedBean the portalManagedBean to set
     */
    public void setPortalManagedBean(FakeMainPortalMB portalManagedBean) {
        this.portalManagedBean = portalManagedBean;
    }
}
